<?php

defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 功能模块管理
 * @exception 400 参数错误
 * @exception 502 服务器错误
 * @exception 503 数据库错误
 */
class ModuleController extends CI_Controller
{

    use noAuth;
    public function __construct()
    {
        parent::__construct();
        $this->load->library('Request');
        $this->load->model('Module');
    }


    /**
     * @return array
     */
    static function getRules()
    {
        return [
            'getModuleList' => [
                'mid' => [
                    'name' => 'mid', 'type' => 'int', 'require' => true, 'desc' => '菜单ID', 'source' => 'get'
                ]
            ],
            'getRightList' => [
                'id' => [
                    'name' => 'id', 'type' => 'int', 'require' => true, 'desc' => '功能模块id', 'source' => 'get'
                ]
            ],
            'addModule' => [
                'name' => [
                    'name' => 'name', 'type' => 'string', 'max' => 45, 'require' => true, 'desc' => '名称(45字符内)', 'source' => 'post'
                ],
                'mid' => [
                    'name' => 'mid', 'type' => 'int', 'require' => true, 'desc' => '菜单ID', 'source' => 'post'
                ],
                'index' => [
                    'name' => 'index', 'type' => 'string', 'max' => 45, 'require' => true, 'desc' => '模块标识,同一个菜单下不能相同', 'source' => 'post'
                ],
                'sort' => [
                    'name' => 'sort', 'type' => 'int', 'require' => true, 'default' => 0, 'desc' => '排序值', 'source' => 'post'
                ]
            ],
            'modModule' => [
                'id' => [
                    'name' => 'id', 'type' => 'int', 'require' => true, 'desc' => '功能模块id', 'source' => 'post'
                ],
                'name' => [
                    'name' => 'name', 'type' => 'string', 'max' => 45, 'require' => true, 'desc' => '名称(45字符内)', 'source' => 'post'
                ],
                'mid' => [
                    'name' => 'mid', 'type' => 'int', 'require' => true, 'desc' => '菜单ID', 'source' => 'post'
                ],
                'index' => [
                    'name' => 'index', 'type' => 'string', 'max' => 45, 'require' => true, 'desc' => '模块标识,同一个菜单下不能相同', 'source' => 'post'
                ],
                'sort' => [
                    'name' => 'sort', 'type' => 'int', 'require' => true, 'default' => 0, 'desc' => '排序值', 'source' => 'post'
                ]
            ],
            'delRight' => [
                'id' => [
                    'name' => 'id', 'type' => 'int', 'require' => true, 'desc' => '功能模块id', 'source' => 'delete'
                ],
                'rightId' => [
                    'name' => 'rightId', 'type' => 'array', 'format' => 'explode', 'separator' => ',', 'require' => true, 'desc' => '接口id（多个以半角 , 分隔）', 'source' => 'delete'
                ]
            ],
            'addRight' => [
                'id' => [
                    'name' => 'id', 'type' => 'int', 'require' => true, 'desc' => '功能模块id', 'source' => 'post'
                ],
                'rightId' => [
                    'name' => 'rightId', 'type' => 'array', 'format' => 'explode', 'separator' => ',', 'require' => true, 'desc' => '接口id（多个以半角 , 分隔）', 'source' => 'post'
                ]
            ]
        ];
    }


    /**
     * 功能模块
     * @desc 获取菜单功能模块列表
     * @return int id 模块组id
     * @return string name 模块组名称
     * @return int sort 模块组排序值
     * @return string index 功能模块标识
     */
    public function getModuleList()
    {
        $data = $this->request->getData();
        $roleList = $this->db->select('id,name,sort,`index`')
            ->where('mid', $data['mid'])
            ->order_by("sort", "asc")
            ->get('module')->result_array();
        restful(200, $roleList);
    }


    /**
     * 功能模块
     * @desc 获取功能模块接口权限列表
     * @throws MyException
     */
    public function getRightList()
    {
        $data = $this->request->getData();
        $rightList = $this->db->select('id, `index`, name')
            ->join('moduleRights', 'moduleRights.rid = rights.id', 'left')
            ->where('moduleRights.mid', $data['id'])
            ->get('rights')->result_array();

        restful(200, $rightList);
    }


    /**
     * 功能模块
     * @desc 添加模块
     * @param
     * @throws MyException
     */
    public function addModule()
    {
        $data = $this->request->getData();
        $checkwhere['mid']=$data['mid'];
        $checkwhere['index']=$data['index'];
        $arr=$this->db->select('id')->where($checkwhere)->get('module')->result_array();
        if(!empty($arr)){
            restful(409, '同菜单下不能存在相同的标识');
        }
        $result = $this->module->add($data['name'],$data['mid'],$data['sort'],$data['index']);
        if ($result !== false) {
            restful(201);
        }else throw new MyException('', MyException::DATABASE);
    }


    /**
     * 功能模块
     * @desc 修改（名称）
     * @throws MyException
     */
    public function modModule()
    {
        $data = $this->request->getData();
        $checkwhere['mid']=$data['mid'];
        $checkwhere['index']=$data['index'];
        $arr=$this->db->select('id')->where($checkwhere)->get('module')->result_array();
        if(count($arr)>1){
            restful(409, '同菜单下不能存在相同的标识');
        }
        $result = $this->module->mod($data['id'], $data['name'],$data['sort'],$data['index']);
        if ($result !== false) {
            restful();
        }else throw new MyException('', MyException::DATABASE);
    }


    /**
     * 功能模块
     * @desc 删除
     * @throws MyException
     */
    public function delModule(int $id=0)
    {
        $result = $this->module->delete($id);
        if ($result !== false) {
            restful();
        }else throw new MyException('', MyException::DATABASE);
    }


    /**
     * 功能模块
     * @desc 删除接口
     * @throws MyException
     */
    public function delRight()
    {
        $data = $this->request->getData();
        $result = $this->module->delRight($data['id'], $data['rightId']);
        if ($result !== false) {
            restful(204, '操作成功');
        }else throw new MyException('', MyException::DATABASE);
    }


    /**
     * 功能模块
     * @desc 添加接口
     * @throws MyException
     */
    public function addRight()
    {
        $data = $this->request->getData();
        $result = $this->module->addRight($data['id'], $data['rightId']);
        if ($result !== false) {
            restful();
        }else throw new MyException('', MyException::DATABASE);
    }

}